package cn.xorange.commons.system.service.platform.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.xorange.commons.core.basic.service.BaseService;
import cn.xorange.commons.core.exception.SysException;
import cn.xorange.commons.system.entity.platform.*;
import cn.xorange.commons.system.entity.system.SysTenantMenu;
import cn.xorange.commons.system.mapper.platform.*;
import cn.xorange.commons.system.mapper.system.SysTenantMenuMapper;
import cn.xorange.commons.system.service.platform.*;
import cn.xorange.commons.core.constants.SysConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author : yangjian
 * date : 2024/6/24
 * description :
 */
@Service
public class SysTenantServiceImpl extends BaseService<SysTenant, SysTenantMapper> implements ISysTenantService {

    @Resource
    SysTenantSpuMapper tSpuMapper;
    @Resource
    SysTenantProductMapper tProductMapper;
    @Resource
    SysTenantMenuMapper tMenuMapper;
    @Resource
    SysTenantUsersMapper tUsersMapper;
    @Resource
    ISysProductSpuService spuService;
    @Resource
    ISysProductService productService;
    @Resource
    ISysProductCycleService cycleService;


    @Override
    public List<SysTenantSpu> getTenantSpu(Long tenantId, String status) {
        return tSpuMapper.selectTenantSpu(tenantId, status);
    }

    // 开通模块功能
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void openTenantSpu(Long tenantId, Long spuId) {
        SysTenantSpu tSpu = new SysTenantSpu();
        tSpu.setSpuId(spuId);
        tSpu.setTenantId(tenantId);
        tSpu = tSpuMapper.select(tSpu);
        SysProductSpu spu = this.spuService.getById(spuId);
        if(Objects.isNull(tSpu)){
            tSpu = new SysTenantSpu();
            tSpu.setSpuId(spuId);
            tSpu.setTenantId(tenantId);
            if(Objects.nonNull(spu.getAgreementId())){
                tSpu.setAgreementId(spu.getAgreementId());
                tSpu.setAgreementTime(new Date());
            }
            tSpu.setStatus(SysConstants.FLAG_SUCCESS);
            tSpuMapper.insert(tSpu);
        }else {
            if(Objects.nonNull(spu.getAgreementId())){
                tSpu.setAgreementTime(new Date());
            }
            tSpu.setStatus(SysConstants.FLAG_SUCCESS);
            tSpu.where.equal("tenantId",tenantId);
            tSpu.where.equal("spuId",spuId);
            tSpuMapper.update(tSpu);
        }
        // 添加默认功能
        if(Objects.nonNull(spu.getProductId())){
            SysTenantProduct tProduct = new SysTenantProduct();
            tProduct.setProductId(spu.getProductId());
            tProduct.setTenantId(tenantId);
            tProduct.setStartDate(new Date());
            Date eDate = DateUtil.parse("3000-12-31 23:59:59");
            tProduct.setEndDate(eDate);
            this.tProductMapper.insert(tProduct);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void closeTenantSpu(Long tenantId, Long spuId) {
        SysTenantSpu tSpu = new SysTenantSpu();
        tSpu.setSpuId(spuId);
        tSpu.setTenantId(tenantId);
        tSpu = tSpuMapper.select(tSpu);
        if(!Objects.isNull(tSpu)){
            tSpu = new SysTenantSpu();
            tSpu.setSpuId(spuId);
            tSpu.setTenantId(tenantId);
            tSpu.setStatus(SysConstants.FLAG_DISABLE);
            tSpu.where.equal("tenantId",tenantId);
            tSpu.where.equal("spuId",spuId);
            tSpuMapper.update(tSpu);
            // 删除模块下所有功能
            SysProduct product = new SysProduct();
            product.setProductSpu(spuId);
            List<SysProduct> productList = this.productService.getList(product);
            if(!productList.isEmpty()){
                Set<Long> productIds = new HashSet<>();
                for(SysProduct p : productList){
                    if(Objects.nonNull(p.getProductId())){
                        productIds.add(p.getProductId());
                    }
                }
                SysTenantProduct tProduct = new SysTenantProduct();
                tProduct.where.equal("tenantId", tenantId);
                tProduct.where.in("productId", productIds);
                this.tProductMapper.del(tProduct);
                // 删除租户自定义的菜单
                List<Long> menuIds = this.productService.getProductMenuIds(productIds);
                if(!menuIds.isEmpty()){
                    SysTenantMenu tMenu = new SysTenantMenu();
                    tMenu.where.equal("tenantId", tenantId);
                    tMenu.where.in("menuId", menuIds);
                    this.tMenuMapper.del(tMenu);
                }

            }

        }
    }

    @Override
    public List<SysTenantUsers> getUsersList(Long tenantId) {
        return tUsersMapper.selectTenantUsersList(tenantId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void setUsers(SysTenantUsers users) {
        SysProductCycle productCycle =  this.cycleService.getById(users.getCycleId());

        if(!productCycle.getProductId().equals(users.getProductId()) ){
            throw SysException.paramException("订购周期与订购用户数不符");
        }

        int cycle = Integer.parseInt(productCycle.getCycleVal());
        Date nowDate = new Date();
        DateTime endDate = DateUtil.offsetMonth(nowDate, cycle);
        users.setStartDate(nowDate);
        users.setEndDate(endDate);
        tUsersMapper.insert(users);
    }

    @Override
    public List<SysTenantProduct> getProductList(Long tenantId) {
        return tProductMapper.selectTenantProductList(tenantId);
    }

    @Override
    public void setProduct(SysTenantProduct product) {
        SysProductCycle productCycle =  this.cycleService.getById(product.getCycleId());

        if(!productCycle.getProductId().equals(product.getProductId()) ){
            throw SysException.paramException("订购周期与订购产品不符");
        }

        int cycle = Integer.parseInt(productCycle.getCycleVal());
        Date nowDate = new Date();
        DateTime endDate = DateUtil.offsetMonth(nowDate, cycle);
        product.setStartDate(nowDate);
        product.setEndDate(endDate);
        tProductMapper.insert(product);
    }


}
